package com.example.zhaojianxu.signin.securities.buy

import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.common.http.HttpUtil
import com.example.zhaojianxu.signin.securities.http.BuyEntity
import kotlinx.android.synthetic.main.activity_buy.*
import kotlinx.android.synthetic.main.common_title_bar.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class BuyActivity : BaseActivity() {

    var mAdapter: BuyAdapter? = null
    val mList: MutableList<BuyEntity> = mutableListOf()

    override fun initView() {
        common_title.text = "股票买进"
        common_setting.visibility = View.GONE
        common_back.setOnClickListener {
            finish()
        }

        mAdapter = BuyAdapter(mList)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        activity_buy_recycler.layoutManager = layoutManager
        activity_buy_recycler.adapter = mAdapter

        activity_buy_swipe.setOnRefreshListener { initData() }
    }

    override fun initData() {

        HttpUtil.instance.getSecuritiesApi()?.postQueryBuy()?.enqueue(object : Callback<MutableList<BuyEntity>>{
            override fun onFailure(call: Call<MutableList<BuyEntity>>, t: Throwable) {

            }

            override fun onResponse(call: Call<MutableList<BuyEntity>>, response: Response<MutableList<BuyEntity>>) {
                val list = response.body()
                if (response.isSuccessful && list != null){

                    val temp = mutableListOf<BuyEntity>()
                    for (position in 0 until list.size){
                        if (list[position].number!! > 0){
                            temp.add(list[position])
                        }
                    }

                    Log.d("TAGGGGG","size = ${response.body()}")
                    mAdapter?.mList = temp
                    mAdapter?.notifyDataSetChanged()
                    activity_buy_swipe.isRefreshing = false
                }
            }

        })


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_buy)
    }
}
